home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Applications / gdbm-1.7.3 / source / conv2gdbm.c < prev    next >
Text File  |  1994-05-21  |  4KB  |  185 lines

  1. /* conv2gdbm.c - This is a program to convert dbm files to gdbm files. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.        
  27. *************************************************************************/
  28.  
  29.  
  30. /* include system configuration before all else. */
  31. #include "autoconf.h"
  32.  
  33. #include <stdio.h>
  34. #if HAVE_STDLIB_H
  35. #include <stdlib.h>
  36. #endif
  37. #include "gdbm.h"
  38.  
  39. #include "getopt.h"
  40.  
  41. extern int dbminit ();
  42. extern datum fetch ();
  43. extern datum firstkey ();
  44. extern datum nextkey ();
  45.  
  46. static void usage();
  47.  
  48. /* Boolean Constants */
  49. #define TRUE 1
  50. #define FALSE 0
  51.  
  52. int
  53. main (argc, argv)
  54.      int argc;
  55.      char *argv[];
  56. {
  57.   GDBM_FILE gfile;    /* The gdbm file. */
  58.   datum key;        /* Key and data pairs retrieved. */
  59.   datum data;
  60.   int   errors;        /* error count. */
  61.   int   num;        /* insert count. */
  62.   int   block_size;     /* gdbm block size. */
  63.   char  quiet;        /* Do work Quietly? */
  64.   char  option_char;    /* The option character. */
  65.  
  66.   char *dbm_file, *gdbm_file;   /* pointers to the file names. */
  67.  
  68.   /* Initialize things. */
  69.   quiet = FALSE;
  70.   block_size = 0;
  71.  
  72.   /* Check for proper arguments. */
  73.  
  74.   if (argc < 2)
  75.     usage (argv[0]);
  76.  
  77.   /* Check for the options.  */
  78.   while  ( (option_char = getopt (argc, argv, "b:q")) != EOF)
  79.     {
  80.       switch (option_char)
  81.     {
  82.     case 'b':
  83.       block_size = atoi (optarg);
  84.       break;
  85.  
  86.     case 'q':
  87.       quiet = TRUE;
  88.       break;
  89.  
  90.     default:
  91.       usage (argv[0]);
  92.     }
  93.     }
  94.  
  95.   /* The required dbm file name. */
  96.   if (argc <= optind)
  97.     {
  98.       usage (argv[0]);
  99.     }
  100.   else
  101.     {
  102.       dbm_file = argv[optind];
  103.       gdbm_file = argv[optind];
  104.       optind += 1;
  105.     }
  106.  
  107.   /* The optional gdbm file name. */
  108.   if (argc > optind)
  109.     {
  110.       gdbm_file = argv[optind];
  111.       optind += 1;
  112.     }
  113.  
  114.   /* No more arguments are legal. */
  115.   if (argc > optind)  usage (argv[0]);
  116.   
  117.   /* Open the dbm file. */
  118.   if (dbminit (dbm_file) != 0)
  119.     {
  120.       printf ("%s: dbm file not opened\n", argv[0]);
  121.       exit (2);
  122.     }
  123.  
  124.   /* Open the gdbm file.  Since the dbm files have .pag and .dir we
  125.      will use the file name without any extension.  */
  126.   gfile = gdbm_open (gdbm_file, block_size, GDBM_WRCREAT, 00664, NULL);
  127.   if (gfile == NULL)
  128.     {
  129.       printf ("%s: gdbm file not opened\n", argv[0]);
  130.       exit (2);
  131.     }
  132.  
  133.  
  134.   /* Do the conversion.  */
  135.   errors = 0;
  136.   num = 0;
  137.  
  138.   if (!quiet)
  139.     printf ("%s: Converting %s.pag and %s.dir to %s.\n", argv[0], dbm_file,
  140.         dbm_file, gdbm_file);
  141.  
  142.   /* The convert loop - read a key/data pair from the dbm file and insert
  143.      it into the gdbm file. */
  144.  
  145.   for (key = firstkey (); key.dptr != NULL; key = nextkey (key))
  146.     {
  147.       data = fetch (key);
  148.       if (gdbm_store (gfile, key, data, GDBM_INSERT) != 0)
  149.     {
  150.       errors++;
  151.     }
  152.       else
  153.     {
  154.       num++;
  155.       if ( !quiet && ((num % 100) == 0))
  156.         {
  157.           printf (".");
  158.           if ( (num % 7000) == 0)
  159.         printf ("\n");
  160.         }
  161.     }
  162.     }
  163.  
  164.   gdbm_close (gfile);
  165.  
  166.   if (!quiet)
  167.     {
  168.       /* Final reporting. */
  169.       if (errors)
  170.     printf ("%s: %d items not inserted into %s.\n", argv[0],
  171.             errors, gdbm_file);
  172.  
  173.       printf  ("%s: %d items inserted into %s.\n", argv[0], num, gdbm_file);
  174.     }
  175.   exit(0);
  176.   /* NOTREACHED */
  177. }
  178.  
  179. static void usage (name)
  180.      char *name;
  181. {
  182.   printf ("usage: %s [-q] [-b block_size] dbmfile [gdbmfile]\n", name);
  183.   exit (2);
  184. }
  185.